#!/bin/zsh

# Variable Replacement tool
REPODIR=$(pwd)
PKGBUILD=$REPODIR/PKGBUILD
setVar() {
  echo 'import("fs").then(fs=>fs.writeFileSync("'"$PKGBUILD"'",fs.readFileSync("'"$PKGBUILD"'","utf-8").replace(/(?<='$1'=")[a-zA-Z0-9_]*(?=")/,"'$2'")))' | node;
}

# Get package info
source $PKGBUILD;

# Set pkgrel
if [[ "$REL" != "" ]]; then
  setVar pkgrel $REL
fi;

# Update the PKGBUILD version
setVar _pkgdate $(date +'%Y%M%d_%H%M_%S');

# Clone the repo
cd /tmp
tmpdir=/tmp/$_reponame-$_pkgname-$_pkgdate
git clone "$_repo" $tmpdir
cd $tmpdir/$_pkgsubdir;

# Update the PKGBUILD commit
setVar _pkgcommit $(git rev-parse --short HEAD);
setVar _commitcount $(git rev-list --all | wc -l);

# Cleanup
rm -rf $tmpdir;

# Re-enter the repo dir
cd $REPODIR;

# Update the package hash sums
updpkgsums;
